/*
 * Implementation of User class.
 * A SASL tutor user.
 */

#include <QtGui>
#include <QtXml>

#include <QtDebug>

#include "includes/user.h"

// constructor
User::User() : lastLogin(1,1,1), dateCreated(1,1,1) {
	name = "";
	imageFilename = "";
}

// parameterised constructor
// note: can't call User() and have objects constructed from init list
User::User(const QString& n) : lastLogin(1,1,1) {
	setName(n);
	// store the date of creation
	dateCreated = QDate::currentDate();
}

// name
const QString& User::getName() const {return name;}
void User::setName(const QString& n) {name = n;}

// image
const QString& User::getImageFilename() const {return imageFilename;}
void User::setImageFilename(const QString& f) {imageFilename = f;}

// last login
const QDate& User::getLastLogin() const {return lastLogin;}
void User::setLastLogin(const QDate& l) {lastLogin = l;}

void User::addProgressItem(const ProgressItem &i)
{
    if (progress.contains(i))
        progress.removeOne(i);
    progress.append(i);
}

QList<ProgressItem> User::getProgress() const
{
    return progress;
}

// date created
const QDate& User::getDateCreated() const {return dateCreated;}

// save as dom node
QDomElement& User::toDomElement (QDomElement& node) {
	// document
	QDomDocument doc = node.ownerDocument();

	// parent
	node.setTagName("user");
	// date of account creation
	node.setAttribute("created", dateCreated.toString("dd/MM/yyyy"));
	// name
	QDomElement nameElement = doc.createElement("name");
	nameElement.appendChild(doc.createTextNode(name));
	node.appendChild(nameElement);
	// image
	QDomElement imageElement = doc.createElement("image");
	imageElement.appendChild(doc.createTextNode(imageFilename));
	node.appendChild(imageElement);
	// last login
	QDomElement lastLoginElement = doc.createElement("last_login");
	lastLoginElement.appendChild(doc.createTextNode(lastLogin.toString("dd/MM/yyyy")));
	node.appendChild(lastLoginElement);

        QDomElement progressElement = doc.createElement("progress");
        foreach (ProgressItem it, progress)
        {
            QDomElement progressItemElement = doc.createElement("progress_item");
            it.toDomElement(progressItemElement);
            progressElement.appendChild(progressItemElement);
        }
        qDebug() << name << " has " << progress.size();
        node.appendChild(progressElement);

	// return reference to same node
	return node;
}

// load from DOM node
void User::fromDomElement(QDomElement& node) {
	if ("user" != node.tagName())
		return;

	// date of creation
	// regex used to reconstruct QDate object from stored string
	QRegExp dateExp("^(\\d\\d)/(\\d\\d)/(\\d\\d\\d\\d)$");
	QString dateString = node.attribute("created");
	// process string
	int pos = dateExp.indexIn(dateString);
	if (pos > -1) {
		dateCreated.setDate(dateExp.cap(3).toInt(), dateExp.cap(2).toInt(), dateExp.cap(1).toInt());
	}

	// name
	name = node.elementsByTagName("name").item(0).firstChild().nodeValue();
	// image path
	imageFilename = node.elementsByTagName("image").item(0).firstChild().nodeValue();
	// last login
	dateString = node.elementsByTagName("last_login").item(0).firstChild().nodeValue();
	pos = dateExp.indexIn(dateString);
	if (pos > -1) {
		lastLogin.setDate(dateExp.cap(3).toInt(), dateExp.cap(2).toInt(), dateExp.cap(1).toInt());
	}
        for (int i = 0; i < node.elementsByTagName("progress_item").size(); i++)
        {
            ProgressItem it;
            QDomElement progressItem = node.elementsByTagName("progress_item").at(i).toElement();
            it.fromDomElement(progressItem);
            this->addProgressItem(it);
        }
}

// equality operator. equal usernames defines equality
bool User::operator== (const User& other) const {
    // compare QStrings
    return (other.getName() == getName());
}
